home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / FPSCounter / FPSCounter.java.z / FPSCounter.java
Encoding:
Java Source  |  2003-08-08  |  9.4 KB  |  267 lines

  1. /*
  2.  *    @(#)FPSCounter.java 1.3 02/04/01 15:04:14
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import javax.media.j3d.*;
  41. import javax.vecmath.*;
  42. import java.text.*;
  43.  
  44. /** This behavior calculates the frame rate and average frame rate of a 
  45.   * Java3D application. 
  46.   * The behavior sets itself up to wakeup every time a new frame is rendered.  
  47.   *
  48.   * <p> The HotSpot(tm) compiler performs some initial optimizations before 
  49.   * running at optimal speed. Frame rates measured during this warmup period 
  50.   * will be inaccurate and not indicative of the true performance of the the 
  51.   * application. Therefore, before beginning the frame rate computation, 
  52.   * the frame counter waits for a fixed time period to allow the HotSpot(tm) 
  53.   * compiler to stablilize. 
  54.   *
  55.   * <p> To avoid computing the frame rate too frequently (which would also 
  56.   * hamper rendering performance), the frame counter only computes the frame 
  57.   * rate at fixed time intervals. The default sampling duration is 10 seconds. 
  58.   * After waiting for the warmup period, the frame counter needs to calibrate 
  59.   * itself. It computes the number of frames rendered during the sampling 
  60.   * period. After doing this calibration, the frame counter reports the frame 
  61.   * rate after these many frames are rendered. It also reports the average 
  62.   * frame rate after a fixed number of sampling intervals (the default is 5). 
  63.   * 
  64.   * <p>The frame counter can be set up to run for a fixed number of sampling 
  65.   * intervals or to run indefinitely. The defaultis to run indefinitely.
  66.   */ 
  67.  
  68. public class FPSCounter extends Behavior {
  69.     // Wakeup condition - framecount = 0 -> wakeup on every frame
  70.     WakeupOnElapsedFrames FPSwakeup = new WakeupOnElapsedFrames(0);
  71.     
  72.     // Do calibration for these many millisec
  73.     private static final long testduration = 1000;    
  74.     
  75.     // Report frame rate after every sampleduration milliseconds
  76.     private static final long sampleduration = 10000;    
  77.     
  78.     // Flag to indicate that it is time to (re)calibrate
  79.     private boolean doCalibration = true;
  80.     
  81.     // Flag to indicate the counter has started
  82.     private boolean startup = true;
  83.     
  84.     // Wait for HotSpot compiler to perform optimizations 
  85.     private boolean warmup = true;
  86.     
  87.     // Time to wait for HotSpot compiler to stabilize (in milliseconds)
  88.     private long warmupTime = 20000; 
  89.     
  90.     // Counter for number of frames rendered
  91.     private int numframes = 0;
  92.     
  93.     // Report frame rate after maxframe number of frames have been rendered
  94.     private int maxframes = 1;
  95.     
  96.     // Variables to keep track of elapsed time
  97.     private long startuptime = 0;
  98.     private long currtime = 0;
  99.     private long lasttime = 0;
  100.     private long deltatime;
  101.     
  102.     // Run indefinitely or for a fixed duration
  103.     private boolean finiteLoop = false;
  104.     
  105.     // No. of sampling intervals to run for if not running indefinitely
  106.     private long maxLoops;
  107.     
  108.     // No. of sampling intervals run for so far
  109.     private long numLoops = 0;
  110.     
  111.     // Total number of frames rendered so far
  112.     private int sumFrames = 0;
  113.     
  114.     // Total time since last reporting of average frame rate
  115.     private long sumTimes = 0;
  116.     
  117.     // Counts no. of sampling intervals 
  118.     private int loop = 0;
  119.     
  120.     // Average frame rate is reported after loopCount number of 
  121.     // sampling intervals 
  122.     private int loopCount = 5;
  123.     private double sumFps = 0.0;
  124.     
  125.     private String symbol[] = {"\\", "|", "|", "/", "-", "|", "-"};     
  126.     int index = 0;
  127.     private NumberFormat nf = null;
  128.     
  129.     public FPSCounter() {
  130.     setEnable(true);
  131.     nf = NumberFormat.getNumberInstance();
  132.     }
  133.     
  134.     // Called to init the behavior
  135.     public void initialize() {
  136.     // Set the trigger for the behavior to wakeup on every frame rendered
  137.     wakeupOn(FPSwakeup);
  138.     }
  139.     
  140.     // Called every time the behavior is activated
  141.     public void processStimulus(java.util.Enumeration critera) {
  142.     // Apply calibration algorithm to determine number of frames to 
  143.     // wait before computing frames per second.
  144.     // sampleduration = 10000 -> to run test, pass for 10 seconds.
  145.     
  146.     if (doCalibration) { // start calibration
  147.             if (startup) {
  148.         // Record time at which the behavior was first invoked
  149.         startuptime = System.currentTimeMillis();
  150.         startup = false;
  151.         }
  152.         else if(warmup) { // Wait for the system to stabilize.
  153.         System.out.print("\rFPSCounter warming up..." + 
  154.             symbol[(index++)%symbol.length]);
  155.         currtime = System.currentTimeMillis();
  156.         deltatime = currtime - startuptime;
  157.         if(deltatime > warmupTime) {
  158.             // Done waiting for warmup
  159.             warmup = false;
  160.             lasttime = System.currentTimeMillis();
  161.             System.out.println("\rFPSCounter warming up...Done");
  162.         }
  163.         }
  164.         else {
  165.         numframes += 1;
  166.         // Wait till at least maxframe no. of frames have been rendered
  167.         if (numframes >= maxframes) {
  168.             currtime = System.currentTimeMillis();
  169.             deltatime = currtime - lasttime;
  170.             // Do the calibration for testduration no. of millisecs
  171.             if (deltatime > testduration) {
  172.             // Compute total no. of frames rendered so far in the 
  173.             // current sampling duration
  174.             maxframes = (int)Math.ceil((double)numframes *
  175.                 ((double)sampleduration /
  176.                  (double)deltatime));
  177.             
  178.             // Done with calibration
  179.             doCalibration = false;
  180.             // reset the value for the measurement
  181.             numframes = 0;
  182.             lasttime = System.currentTimeMillis();
  183.             }
  184.             else {
  185.             // Need to run the calibration routine for some more 
  186.             // time. Increase the no. of frames to be rendered
  187.             maxframes *= 2;
  188.             }
  189.         }
  190.         }
  191.     }
  192.     else { // do the measurement
  193.         numframes += 1;
  194.         if (numframes >= maxframes) {
  195.         currtime = System.currentTimeMillis();
  196.         deltatime = currtime - lasttime;
  197.         // time is in millisec, so multiply by 1000 to get frames/sec
  198.         double fps = (double)numframes / ((double)deltatime / 1000.0);
  199.         
  200.         System.out.println("Frame Rate : \n\tNo. of frames : " + 
  201.             numframes + "\n\tTime : " + 
  202.             ((double)deltatime / 1000.0) +
  203.             " sec." + "\n\tFrames/sec : " + nf.format(fps));
  204.         
  205.         // Calculate average frame rate
  206.         sumFrames += numframes;
  207.         sumTimes += deltatime;
  208.         sumFps += fps;
  209.         loop++;
  210.         if (loop >= loopCount) {
  211.             double avgFps = (double)sumFrames*1000.0/(double)sumTimes;
  212.             double ravgFps = sumFps/(double)loopCount;
  213.             System.out.println("Aggregate frame rate " + 
  214.                 nf.format(avgFps) + " frames/sec");
  215.             System.out.println("Average frame rate " + 
  216.                 nf.format(ravgFps) + " frames/sec");
  217.             numLoops++;
  218.             if (finiteLoop && numLoops >= maxLoops) {
  219.             System.out.println("************** The End **************\n");
  220.             setEnable(false);
  221.             }
  222.             loop = 0;
  223.             sumFps = 0;
  224.         }
  225.         numframes = 0;
  226.         lasttime = System.currentTimeMillis();;
  227.         }
  228.     }
  229.     // Set the trigger for the behavior
  230.     wakeupOn(FPSwakeup);
  231.     }
  232.     
  233.     /** The frame counter waits for some time before computing the frame rate. 
  234.       * This allows the HotSpot compiler to perform initial optimizations. 
  235.       * The amount of time to wait for is set by this method. The default is 
  236.       * 20000 (20 sec)
  237.       * @param Amount of time to wait for before computing frame rate 
  238.       * (specified in milliseconds)
  239.       */
  240.     public void setWarmupTime(long wt) {
  241.     warmupTime = wt;
  242.     }
  243.     
  244.     /** Sets the number of sampling intervals to wait for before computing 
  245.       * the average frame rate.
  246.      * The default is 5.
  247.      * @param No. of sampling intervals over which to compute frame rate. 
  248.      * A value of 0 implies the average frame rate is computed over one 
  249.      * sampling interval 
  250.      */
  251.     public void setLoopCount(int lc) {
  252.     loopCount = lc;
  253.     }
  254.     
  255.     /** This method sets the number of sampling intervals for which the frame 
  256.       * counter should run. 
  257.      * @param No. of sampling intervals to run for
  258.      */
  259.     public void setMaxLoops(int ml) {
  260.     maxLoops = ml;
  261.     finiteLoop = true;
  262.     }    
  263.     
  264. }
  265.  
  266.  
  267.